package cl.blueprintsit.utils.pdf;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import org.apache.log4j.Logger;

import java.io.*;

import static com.itextpdf.text.pdf.PdfWriter.*;

/*import org.docx4j.convert.out.pdf.viaXSLFO.PdfSettings;
import org.docx4j.fonts.IdentityPlusMapper;
import org.docx4j.fonts.Mapper;
import org.docx4j.fonts.PhysicalFont;
import org.docx4j.fonts.PhysicalFonts;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;*/

/**
 * Created with IntelliJ IDEA.
 * User: afarias
 * Date: 11/6/13
 * Time: 8:18 AM
 * To change this template use File | Settings | File Templates.
 */
public class PDFUtils {

    private static Logger logger = Logger.getLogger(PDFUtils.class);

    public static File protectPDF(byte[] pdfContent, File protectedPDF,
                                  String password) throws FileNotFoundException, DocumentException {
        FileOutputStream fosPDF = new FileOutputStream(protectedPDF);
        PdfReader pdfReader;
        PdfStamper stamper;
        try {
            pdfReader = new PdfReader(pdfContent);
            stamper = new PdfStamper(pdfReader, fosPDF);
        } catch (IOException e) {
            String errorMsg = "There was an error while reading the pdf content";
            logger.error(errorMsg);
            throw new DocumentException(errorMsg);
        }

        /* The encryption parameters are set */
        stamper.setEncryption(password.getBytes(), password.getBytes(), ALLOW_PRINTING,
                ENCRYPTION_AES_128 | DO_NOT_ENCRYPT_METADATA);
        try {
            stamper.close();
        } catch (IOException e) {
            logger.error("There was a problem while encrypting the PDF: " + pdfReader);
            e.printStackTrace();
        }

        pdfReader.close();
        return protectedPDF;
    }


    public static File protectPDF(File pdfFile, File protectedPDF, String password) throws FileNotFoundException,
            DocumentException {
        FileOutputStream fosPDF = new FileOutputStream(protectedPDF);
        PdfReader pdfReader;
        PdfStamper stamper;
        try {
            pdfReader = new PdfReader(new FileInputStream(pdfFile));
            stamper = new PdfStamper(pdfReader, fosPDF);
        } catch (IOException e) {
            logger.error("There was an error while reading the file " + pdfFile.getName());
            throw new DocumentException();
        }

        /* The encryption parameters are set */
        stamper.setEncryption(password.getBytes(), password.getBytes(), ALLOW_PRINTING,
                ENCRYPTION_AES_128 | DO_NOT_ENCRYPT_METADATA);
        try {
            stamper.close();
        } catch (IOException e) {
            logger.error("There was a problem while encrypting the PDF: " + pdfReader);
            e.printStackTrace();
        }

        pdfReader.close();
        return protectedPDF;
    }

   /* public static File doc2pdf(File aPDF) throws Exception {

        WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(aPDF);

        // Set up font mapper
        Mapper fontMapper = new IdentityPlusMapper();
        wordMLPackage.setFontMapper(fontMapper);

        // Example of mapping missing font Algerian to installed font Comic Sans MS
        PhysicalFont font
                = PhysicalFonts.getPhysicalFonts().get("Comic Sans MS");
        fontMapper.getFontMappings().put("Algerian", font);

        org.docx4j.convert.out.pdf.PdfConversion c
                = new org.docx4j.convert.out.pdf.viaXSLFO.Conversion(wordMLPackage);
        //  = new org.docx4j.convert.out.pdf.viaIText.Conversion(wordMLPackage);

        File output = new File("/Users/afarias/IdeaProjects/MetLife Services/DocumentService/out/" + "test.pdf");
        OutputStream os = new java.io.FileOutputStream(output);
        PdfSettings pdfSettings = new PdfSettings();
        c.output(os, pdfSettings);

        return output;
    }*/
}
